package com.zxjbyte.yiyi.framework.oss.event;

import cn.hutool.extra.cglib.CglibUtil;
import com.zxjbyte.yiyi.module.system.api.oss.OssRecordApi;
import com.zxjbyte.yiyi.module.system.domain.dto.oss.OssRecordAddDTO;
import com.zxjbyte.yiyi.module.system.domain.dto.oss.OssRecordDelDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;

/**
 * 这里记录Oss上传日志记录使用spring机制的好处是：
 *
 * 可以提供松耦合、简化代码、增强可扩展性和可测试性等，使得应用程序更加灵活、可维护和可扩展
 * @Author zhangxingjia
 * @Date 2023/6/26 15:14
 * @Version: 1.0
 */
@RequiredArgsConstructor
public class OssRecordListener {

    private final OssRecordApi ossRecordApi;

    @Order
    @EventListener(value = OssRecordAddEvent.class)
    public void addOperateLog(OssRecordAddEvent event) {
        OssRecordAddDTO addDto = CglibUtil.copy(event, OssRecordAddDTO.class);
        ossRecordApi.asyncAdd(addDto);
    }


    @Order
    @EventListener(value = OssRecordDeleteEvent.class)
    public void deleteOperateLog(OssRecordDeleteEvent event) {
        OssRecordDelDTO delDto = CglibUtil.copy(event, OssRecordDelDTO.class);
        ossRecordApi.asyncDeleteByPath(delDto);
    }
}
